package com.gdpu.liou.symphonyserver.common.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.UUID;

import static com.gdpu.liou.symphonyserver.common.utils.LIUUtil.getParams;

/**
 * 拦截器
 *
 * @author liuyx
 * @date 2022/5/18 16:29 周三
 */
@Slf4j
@Component
public class SecurityInterceptor implements HandlerInterceptor {
    private int count = 0;
    /**
     * 在Controller方法前进行拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 获取请求ip
        String ip = request.getRemoteAddr();
        // 获取请求url
        String url = request.getRequestURL().toString();
        // 获取请求方法
        String method = request.getMethod();
        // 获取请求参数
        HashMap<String, String> params = getParams(request);
        // 设置bizSeq流水号 串联内部日志流水号
        MDC.put("bizSeq", UUID.randomUUID().toString() + "-" + count++);
        log.info("IP:{}  URL:{}  Method:{}  Params:{}", ip, url, method, params);
        return true;
    }

}
